<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Formatter;

use App\Model\Reply;
use Hyperf\Di\Annotation\Inject;
use HyperfX\Utils\Service;

class ReplyFormatter extends Service
{
    /**
     * @Inject
     * @var UserFormatter
     */
    protected $user;

    public function base(Reply $model)
    {
        return [
            'id' => $model->id,
            'question_id' => $model->question_id,
            'answer_id' => $model->answer_id,
            'user_id' => $model->user_id,
            'summary' => $model->summary,
        ];
    }

    public function info(Reply $model)
    {
        $result = $this->base($model);
        if ($model->user) {
            $result['user'] = $this->user->small($model->user);
        }

        return $result;
    }

    public function formatList($models, bool $info = false)
    {
        $result = [];
        foreach ($models as $model) {
            $result[] = $info ? $this->info($model) : $this->base($model);
        }

        return $result;
    }
}
